<template>
  <el-row class="tac">
    <el-menu
      default-active="2"
      class="el-menu-vertical-demo"
      @open="handleOpen"
      @close="handleClose"
      background-color="#545c64"
      text-color="#fff"
      active-text-color="#ffd04b"
      :collapse="isCollapse"
    >
      <h3>{{ isCollapse ? "后台" : "通用后台管理系统" }}</h3>

      <el-menu-item
        @click="clickMenu(item)"
        v-for="item in noChild"
        :index="item.path"
        :key="item.path"
      >
        <i :class="'el-icon-' + item.icon"></i>
        <span slot="title">{{ item.label }}</span>
      </el-menu-item>
      <!-- 报错:Invalid prop: custom validator check failed for prop "index".-->
      <!-- 原因：elementUI的index是字符串格式，加一个‘ ’，将后台数据转换为字符串 -->
      <el-submenu
        v-for="item in hasChild"
        :index="item.path + ''"
        :key="item.path"
      >
        <template slot="title">
          <i :class="'el-icon-' + item.icon"></i>
          <span>{{ item.label }}</span>
        </template>
        <el-menu-item-group
          v-for="(subItem, subIndex) in item.children"
          :key="subIndex"
        >
          <el-menu-item @click="clickMenu(subItem)" :index="subIndex + ''">
            <i :class="'el-icon-' + subItem.icon"></i>
            {{ subItem.label }}
          </el-menu-item>
        </el-menu-item-group>
      </el-submenu>

      <el-menu-item index="3" disabled>
        <i class="el-icon-document"></i>
        <span slot="title">导航三</span>
      </el-menu-item>

      <el-menu-item index="4">
        <i class="el-icon-setting"></i>
        <span slot="title">导航四</span>
      </el-menu-item>
    </el-menu>
  </el-row>
</template>

<script>
export default {
  name: "Aside",
  data() {
    return {
      menu: [],
    };
  },
  methods: {
    handleOpen(key, keyPath) {
      console.log(key, keyPath);
    },
    handleClose(key, keyPath) {
      console.log(key, keyPath);
    },
    clickMenu(item) {
      this.$router.push({
        name: item.name,
      });
      this.$store.commit("selectMenu", item);
    },
  },
  computed: {
    noChild() {
      return this.asyncMenu.filter((item) => !item.children);
    },
    hasChild() {
      return this.asyncMenu.filter((item) => item.children);
    },
    isCollapse() {
      return this.$store.state.tab.isCollapse;
    },
    asyncMenu(){
      return this.$store.state.tab.menu
    }
  },
};
</script>

<style lang="less" scoped>
.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 200px;
  min-height: 400px;
}
.el-menu-vertical-demo {
  // width:200px;
  // width: 200px!important;
  height: 100vh;
}
.el-menu {
  border: none;
  h3 {
    text-align: center;
    margin: 0;
    line-height: 60px;
    color: aliceblue;
  }
}
</style>

